<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!-- 自动识别最新稳定版的Vue.js  -->
    <script src="vue.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>
<body xmlns:v-on="http://www.w3.org/1999/xhtml">
    <div id="app">

    </div>
<div id="app1">
    <el-tabs v-model="editableTabsValue" type="card" editable @edit="handleTabsEdit">
        <el-tab-pane
                :key="item.name"
                v-for="(item, index) in editableTabs"
                :label="item.title"
                :name="item.name"
        >
            {{item.content}}
        </el-tab-pane>
    </el-tabs>
    <script>
        export default {
            data() {
                return {
                    editableTabsValue: '2',
                    editableTabs: [{
                        title: 'Tab 1',
                        name: '1',
                        content: 'Tab 1 content'
                    }, {
                        title: 'Tab 2',
                        name: '2',
                        content: 'Tab 2 content'
                    }],
                    tabIndex: 2
                }
            },
            methods: {
                handleTabsEdit(targetName, action) {
                    if (action === 'add') {
                        let newTabName = ++this.tabIndex + '';
                        this.editableTabs.push({
                            title: 'New Tab',
                            name: newTabName,
                            content: 'New Tab content'
                        });
                        this.editableTabsValue = newTabName;
                    }
                    if (action === 'remove') {
                        let tabs = this.editableTabs;
                        let activeName = this.editableTabsValue;
                        if (activeName === targetName) {
                            tabs.forEach((tab, index) => {
                                if (tab.name === targetName) {
                                    let nextTab = tabs[index + 1] || tabs[index - 1];
                                    if (nextTab) {
                                        activeName = nextTab.name;
                                    }
                                }
                            });
                        }

                        this.editableTabsValue = activeName;
                        this.editableTabs = tabs.filter(tab => tab.name !== targetName);
                    }
                }
            }
        }
    </script>
</div>
</body>
<script>
    /*必须有vue*/
    new Vue({
        el:'#app'
    })
</script>
</html>